from sqlalchemy import event
from sqlmodel import Field
from sqlalchemy import event

from models.Base import Base
from models.common import get_current_time

# 创建附件表
class Attachment(Base, table=True):
    __tablename__ = "sys_attachment"  # 确保表名一致
    category: str = Field(default="", title="类别", index=True)
    admin_id: int = Field(default=0, title="管理员ID")
    user_id: int = Field(default=0, title="会员ID")
    url: str = Field(default="", title="物理路径")
    imagewidth: str = Field(default="", title="宽度")
    imageheight: str = Field(default="", title="高度")
    imagetype: str = Field(default="", title="图片类型")
    imageframes: int = Field(default=0, title="图片帧数")
    filename: str = Field(default="", title="文件名称")
    filesize: int = Field(default=0, title="文件大小")
    mimetype: str = Field(default="", title="mime类型")
    extparam: str = Field(default="", title="透传数据")
    createtime: int = Field(default=get_current_time(), title="创建日期")
    updatetime: int = Field(default=get_current_time(), title="更新时间"
                            )
    uploadtime: int = Field(default=get_current_time(), title="上传时间")
    storage: str = Field(default="local", title="存储位置")

    # TODO: 关联用户表
    # user: Optional["User"] = Relationship(back_populates="attachments")
    # TODO: 关联管理员表
    # admin: Optional["Admin"] = Relationship(back_populates="attachments")

@event.listens_for(Attachment, 'before_update', propagate=True)
def timestamp_before_update(mapper, connection, target):
    target.updatetime = get_current_time()

@event.listens_for(Attachment, 'before_insert', propagate=True)
def timestamp_before_insert(mapper, connection, target):
    target.createtime = get_current_time()
    target.updatetime = get_current_time()
    target.uploadtime = get_current_time()



# scipter